﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2/15/2011
 * Time: 7:53 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using TomatoSynchronizer.Store;
using TomatoSynchronizer.Forms;
using FileSystemDatabase;
using System.Web.Script.Serialization;

namespace TomatoSynchronizer.Forms
{
	/// <summary>
	/// Description of SyncProfileForm.
	/// </summary>
	public partial class SyncProfileForm : Form
	{
		private HtmlWeb _client;
		
		public SyncProfileForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			_client = new HtmlWeb();			
		}
		
		void TabPage1Click(object sender, EventArgs e)
		{
			
		}
		
		void TreeView1AfterSelect(object sender, TreeViewEventArgs e)
		{
			
		}
		
		void SyncProfileFormLoad(object sender, EventArgs e)
		{
			
		}
		
		private void BuildTree(Site site)
		{
			List<Dictionary<string,object>> accesses = GetAccesses(site);
			
			foreach(Dictionary<string,object> access in accesses)
			{
				foreach(string cle2 in access.Keys)
				{
					if(cle2 == "children")
					{
						List<Dictionary<string,object>> children = (List<Dictionary<string,object>>)access[cle2];
						
						foreach(Dictionary<string,object> child in children)
						{
							TreeNode node = new TreeNode(child["text"].ToString());
							node.Name = child["id"].ToString();
							treeView1.Nodes.Add(node);
						}
					}
				}
			}
		}
		
		private List<Dictionary<string,object>> GetAccesses(Site _site)
		{
			List<Dictionary<string,object>> Accesses = new List<Dictionary<string,object>>();
			string url = _site.Url + "/admin/json.php?action=get_accesses&module=administrators&node=root";
			
			HtmlAgilityPack.HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			string response = "{records:" + node.InnerText + "}";
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(response);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> Access = (Dictionary<string,object>)recs[i];
						List<Dictionary<string,object>> children = new List<Dictionary<string,object>>();
						
						foreach(string cle in Access.Keys)
						{
							switch(cle)
							{
								case "children":
									ArrayList recs1 = (ArrayList)Access[cle];
									for(int j = 0;j < recs1.Count;j++)
									{
										Dictionary<string,object> child = (Dictionary<string,object>)recs1[j];
										children.Add(child);
									}
									break;
							}
						}
						
						Access["children"] = children;
						Accesses.Add(Access);
					}
				}
			}
			
			return Accesses;
		}
		
		void BtnTestConnectionClick(object sender, EventArgs e)
		{
			Site src = new Site(txtNomSiteSrc.Text,txtUrlSrc.Text,txtUserSrc.Text,txtPassSrc.Text);
			
			if(src.Login(_client))
			{
				BuildTree(src);
			}
			else
			{
				MessageBox.Show("PB");
			}
		}
		
		void BtnCloseClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void BtnSaveClick(object sender, EventArgs e)
		{
			SyncProfile profile = new SyncProfile(txtProfileName.Text);
			Site src = new Site(txtNomSiteSrc.Text,txtUrlSrc.Text,txtUserSrc.Text,txtPassSrc.Text);
			Site dest = new Site(txtSiteNameDest.Text,txtUrlDest.Text,txtUserDest.Text,txtPassDest.Text);
			profile.Src = src;
			profile.Dest = dest;
			
			foreach(TreeNode node in treeView1.Nodes)
			{
				if(node.Checked)
				{
					TomatoReplica replica = new TomatoReplica(node.Name,node.Text);
					profile.Replicas.Add(replica);
				}
			}
			
			JsonDatabase db = new JsonDatabase("profiles");
			db.Write<SyncProfile>(profile,profile.Name);
			MessageBox.Show("Profile saved");
		}
		
		void BtnSelectAllClick(object sender, EventArgs e)
		{
			foreach(TreeNode node in treeView1.Nodes)
			{
				node.Checked = true;
			}
		}
		
		void BtnDeselectAllClick(object sender, EventArgs e)
		{
			foreach(TreeNode node in treeView1.Nodes)
			{
				node.Checked = false;
			}
		}
	}
}
